import {
  getWeek,
  getWeekDay,
  getMonthDate,
  month
} from '@/utils/fromDate.js'
/**
 * 传入一个月的tally数据进行整理返回
 * @param {Array} day 一个月的tally数据
 * @return 整理好的tally数据
 */
export function dayConsume(day) {
  let tally
  day.sort(function(a, b) {
    return parseInt(a.day) - parseInt(b.day)
  })
  return day.map((item, index, arr) => {
    let {
      year,
      month,
      day,
      _id,
      icon,
      name,
      value,
      type,
      money
    } = item
    try {
      if (item.day != arr[index - 1].day) {
        tally = {
          week: getWeek(year + '-' + month + '-' + day),
          date: `${month}-${day}`,
          array: [{
            id: _id,
            icon,
            name,
            type,
            money,
            value
          }]
        }
        return tally
      } else {
        tally.array.push({
          id: _id,
          icon,
          name,
          type,
          money,
          value
        })
      }
    } catch (e) {
      // console.log(e);
      //TODO handle the exception
      tally = {
        week: getWeek(year + '-' + month + '-' + day),
        date: `${month}-${day}`,
        array: [{
          id: _id,
          icon,
          name,
          type,
          money,
          value
        }],
      }
      return tally
    }
  }).filter(Boolean)
}
/**
 * 统计一个周每天支出|收入了多少
 * @param {Number} type 0|1 支出|收入
 * @param {Array} week 一个周的tally数据 
 * @return {Array} 返回一个周支出|或收入数据
 */
export function weekData(week, type) {
  // let weekArray = []
  if (type === 0) {
    let arr = week.filter((v, i) => {
      return v.type != 1
    })
    // console.log(arr);
    return countWeekData(arr)
  } else if (type === 1) {
    let arr = week.filter((v, i) => {
      return v.type != 0
    })
    return countWeekData(arr)
  } else {
    return []
  }
}

/**
 * 统计一个周的数据
 * @param {Object} dataArr
 */
function countWeekData(dataArr) {
  // console.log(dataArr);
  let week = []
  for (let i = 0; i < dataArr.length; i++) {
    if (dataArr[i]) {
      week = getWeekDay(`${dataArr[0].year}-${dataArr[0].month}-${dataArr[0].day}`).map(v => {
        let arr = v.split('-')
        return arr[2].toString().padStart(2, '0')
      })
    }
  }
  // console.log(week);
  let weekMoney = new Array(0, 0, 0, 0, 0, 0, 0)
  for (let i = 0; i < dataArr.length; i++) {
    if (dataArr[i].money) {
      switch (dataArr[i].day) {
        case week[0]:
          weekMoney[0] += parseFloat(dataArr[i].money)
          break
        case week[1]:
          weekMoney[1] += parseFloat(dataArr[i].money)
          break
        case week[2]:
          weekMoney[2] += parseFloat(dataArr[i].money)
          break
        case week[3]:
          weekMoney[3] += parseFloat(dataArr[i].money)
          break
        case week[4]:
          weekMoney[4] += parseFloat(dataArr[i].money)
          break
        case week[5]:
          weekMoney[5] += parseFloat(dataArr[i].money)
          break
        case week[6]:
          weekMoney[6] += parseFloat(dataArr[i].money)
          break
      }
    }
  }
  // console.log(weekMoney);
  return weekMoney.map(v=>{
    return parseFloat(v.toFixed(2))
  })
}

/**
 * 统计本月的消费和收入
 * @param {Object} data 本月的数据
 */
export function countMonth(data, type) {
  let days = getMonthDate()
  // console.log(days);
  if (type == 0) {
    data = data.filter(v => v.type == 0)
    return count(data,days,'day')
  } else if (type == 1) {
    data = data.filter(v => v.type == 1)
    // console.log(data);
    return count(data,days,'day')
  } else {
    return []
  }
}

export function countYear(data,type){
  let months = ['01','02','03','04','05','06','07','08','09','10','11','12']
  if (type == 0) {
    data = data.filter(v => v.type == 0)
    return count(data,months,'month')
  } else if (type == 1) {
    data = data.filter(v => v.type == 1)
    // console.log(data);
    return count(data,months,'month')
  } else {
    return []
  }
}


/**
 * 统计各种类型的金额总额
 * @param {Object} data 统计的数据
 * @param {Object} typeArr 统计的类型数组
 * @param {Object} type 统计的类型
 */
function count(data, typeArr, type) {
  let money = Array(typeArr.length).fill(0);

  for (let i = 0; i < data.length; i++) {
    for (let j = 0; j < typeArr.length; j++) {
      if (data[i][type] == typeArr[j]) {
        money[j] += parseFloat(data[i].money)
      }
    }
  }
  return money.map(v=>{
    return parseFloat(v.toFixed(2))
  })
}

/**
 * 
 */
export class chart {
  // 整理周的chart数据格式
  weekChart(data, lastData) {
    // console.log(data,lastData);
    let chart = {
      categories: ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期天"],
      series: [{
          name: "本周",
          data
        },
        {
          name: "上周",
          data: lastData
        }
      ]
    }
    return chart
  }
  // 整理月的折线图
  monthChart(data){
    let categories = getMonthDate()
    return {
      categories,
      series: [{
          name: "本月",
          data
        }
      ]
    }
  }
  // 整理年的折线图
  yearChart(data){
    let categories = ['01','02','03','04','05','06','07','08','09','10','11','12']
    return {
      categories,
      series:[{
        name:'今年',
        data
      }]
    }
  }
  // 整理饼图
  chartPie(data, type = -1) {
    if (type == 0) {
      data = data.filter(v => {
        return v.type == 0
      })
    } else if (type == 1) {
      data = data.filter(v => {
        return v.type == 1
      })
    }
    // 消费类型
    // console.log(data);
    let name = [...new Set(data.map(v => {
      return v.name
    }))]
    // console.log(name);
    // 消费金额默认 0
    let money = Array(name.length).fill(0);
    for (let i = 0; i < data.length; i++) {
      for (let j = 0; j < name.length; j++) {
        if (data[i].name == name[j]) {
          money[j] += parseFloat(data[i].money)
        }
      }
    }
    let moneyArr = name.map((v, i) => {
      return {
        name: v,
        value: money[i]
      }
    })
    return {
      series: [{
        data: moneyArr
      }]
    }
  }
}
